package cn.labbook.bluetooth.controller;


import cn.labbook.bluetooth.common.ResultDto;
import cn.labbook.bluetooth.entity.Ammeter;
import cn.labbook.bluetooth.entity.LadderScheme;
import cn.labbook.bluetooth.service.IAmmeterService;
import cn.labbook.bluetooth.service.ILadderSchemeService;
import cn.labbook.bluetooth.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lonzy
 * @since 2019-05-14
 */
@Controller
@RequestMapping("//ladderScheme")
public class LadderSchemeController extends AbstractController{
    @Autowired
    private ILadderSchemeService ladderSchemeService;
    @Autowired
    private IAmmeterService ammeterService;

    @RequestMapping("search")
    @ResponseBody
    public ResultDto searchElectricity(String ammeterId){
        LambdaQueryWrapper<LadderScheme> lambda = new QueryWrapper<LadderScheme>().lambda();
        lambda.eq(StringUtils.isNotBlank(ammeterId),LadderScheme::getAmmeterId,ammeterId);
        LadderScheme one = ladderSchemeService.getOne(lambda);
        if(one == null || one.getLadderStatus() ==0){
            LambdaQueryWrapper<Ammeter> lambda1 = new QueryWrapper<Ammeter>().lambda();
            lambda1.eq(StringUtils.isNotBlank(ammeterId),Ammeter::getAmmeterId,ammeterId);
            Ammeter one1 = ammeterService.getOne(lambda1);
            if(one == null){
                one = new LadderScheme();
                one.setDayPrice(one1.getDayPrice());
                one.setNightPrice(one1.getNightPrice());
                one.setEffectTime(one1.getEffectTime());
                one.setProgram(0);
            }else{
                one.setDayPrice(one1.getDayPrice());
                one.setNightPrice(one1.getNightPrice());
                one.setEffectTime(one1.getEffectTime());
                one.setProgram(0);
            }
        }else{
            one.setProgram(1);
        }
        return success(one);
    }

    @RequestMapping("set")
    @ResponseBody
    public ResultDto setElectricity(LadderScheme ladderScheme){
        boolean b=false;
        if(ladderScheme.getProgram()==1){
            b = ladderSchemeService.updateById(ladderScheme);
            String ammeterId = ladderScheme.getAmmeterId();
            Ammeter ammeter = ammeterService.getById(ammeterId);
            ammeter.setDayPrice(0.00);
            ammeter.setNightPrice(0.00);
            ammeterService.updateById(ammeter);
        }else if(ladderScheme.getProgram()==0){
            String ammeterId = ladderScheme.getAmmeterId();
            Ammeter ammeter = ammeterService.getById(ammeterId);
            Double dayPrice = ladderScheme.getDayPrice();
            Double nightPrice = ladderScheme.getNightPrice();
            ammeter.setDayPrice(dayPrice);
            ammeter.setNightPrice(nightPrice);
            ammeter.setEffectTime(ladderScheme.getEffectTime());
            b = ammeterService.updateById(ammeter);
        }
        return b?success(b):fail("设置失败");
    }

}
